package com.zxk.crawler;

import com.alibaba.fastjson.JSON;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.zxk.crawler.task.NovelProcess;
import okhttp3.*;
import org.apache.commons.io.FileUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.jetbrains.annotations.NotNull;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.junit.jupiter.api.Test;
import org.springframework.util.DigestUtils;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.HtmlNode;
import us.codecraft.webmagic.selector.Selectable;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description TODO
 * @Author zxk
 * @DATE 2023/4/7 12:29
 * @Version 1.0
 **/
public class CommonTask {


    @Test
    public void test1() {
        NovelProcess novelProcess = new NovelProcess();
        novelProcess.novelProcessor();
    }

    @Test
    public void test2() throws IOException {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost();

        httpPost.addHeader("Content-Type","application/x-www-form-urlencoded");
        httpPost.addHeader("Cookie","_abcde_qweasd=0; Hm_lvt_e72c2efb8449211f96b48f51b055b732=1680841902; Hm_lpvt_e72c2efb8449211f96b48f51b055b732=1680841933");
        httpPost.addHeader("Sec-Fetch-User","?1");
        httpPost.addHeader("Upgrade-Insecure-Requests","1");

        Map<String,Object> map = new HashMap<>();
        map.put("searchkey","星辰");

//        HttpEntity httpEntity = new StringEntity(JSON.toJSONString(map),"application/json","utf-8");
        HttpEntity httpEntity = new StringEntity(JSON.toJSONString(map),"utf-8");
        httpPost.setEntity(httpEntity);

        CloseableHttpResponse response = httpClient.execute(httpPost);

        System.out.println(JSON.toJSONString(response));
    }

    @Test
    public void test3() throws IOException {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        RequestBody body = RequestBody.create(mediaType, "searchkey=星辰");
        Request request = new Request.Builder()
                .url("https://www.ibiquge.la/modules/article/waps.php")
                .method("POST", body)
                .addHeader("Sec-Fetch-User", "?1")
                .addHeader("Upgrade-Insecure-Requests", "1")
                .addHeader("Cookie", "_abcde_qweasd=0; Hm_lvt_e72c2efb8449211f96b48f51b055b732=1680841902; Hm_lpvt_e72c2efb8449211f96b48f51b055b732=1680841933")
                .addHeader("User-Agent", "Apifox/1.0.0 (https://www.apifox.cn)")
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .addHeader("Accept", "*/*")
                .addHeader("Host", "www.ibiquge.la")
                .addHeader("Connection", "keep-alive")
                .build();
        Response response = client.newCall(request).execute();
        System.out.println(JSON.toJSONString(response));
    }

    @Test
    public void test4() throws UnirestException {
        Unirest.setTimeouts(0, 0);
        HttpResponse<String> response = Unirest.post("https://www.ibiquge.la/modules/article/waps.php")
                .header("Sec-Fetch-User", "?1")
                .header("Upgrade-Insecure-Requests", "1")
                .header("Cookie", "_abcde_qweasd=0; Hm_lvt_e72c2efb8449211f96b48f51b055b732=1680841902; Hm_lpvt_e72c2efb8449211f96b48f51b055b732=1680841933")
                .header("User-Agent", "Apifox/1.0.0 (https://www.apifox.cn)")
                .header("Content-Type", "application/x-www-form-urlencoded")
                .header("Accept", "*/*")
                .header("Host", "www.ibiquge.la")
                .header("Connection", "keep-alive")
                .field("searchkey", "星辰")
                .asString();

        System.out.println(JSON.toJSONString(response));
    }

    @Test
    public void test5() throws UnirestException, IOException {
        String url = "https://www.ibiquge.la/40/40401";
        Html html = getHtml(url);
        Selectable node = html.css("#list > dl > dd");
        List<Selectable> nodes = ((HtmlNode) node).nodes();
        List<String> urlList = new ArrayList<>();
        for (Selectable selectable : nodes) {
//            System.out.println(selectable.css("dd > a", "href").toString());
            urlList.add("https://www.ibiquge.la"+selectable.css("dd > a", "href").toString());
        }

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 1 ; i++) {
            Html html1 = getHtml(urlList.get(i));
            String str = html1.css("#content", "text").toString();
            sb.append(str);
        }

        String content = sb.toString();
        String str = "20190805000324150"+content+"6"+"F129Ml29L7RABB1YlZli";
        String s = DigestUtils.md5DigestAsHex(str.getBytes());
        System.out.println(s);

        Unirest.setTimeouts(0, 0);
        HttpResponse<String> response = Unirest.post("https://fanyi-api.baidu.com/api/trans/vip/translate?q=%E4%BD%A0%E5%A5%BD&from=zh&to=en&appid=20190805000324150&salt=6&sign=93f07dc0eab634ef283d1c922fc92910")
                .header("User-Agent", "Apifox/1.0.0 (https://www.apifox.cn)")
                .header("Content-Type", "application/x-www-form-urlencoded")
                .header("Accept", "*/*")
                .header("Host", "fanyi-api.baidu.com")
                .header("Connection", "keep-alive")
                .asString();

        System.out.println(JSON.toJSONString(response));
//        FileUtils.writeStringToFile(new File("D:\\file\\zxk\\gogog\\测试1.txt"),sb.toString());

    }

    @NotNull
    private Html getHtml(String url) throws UnirestException {
        Unirest.setTimeouts(0, 0);
        //"https://www.ibiquge.la/40/40401/18554869.html"
        HttpResponse<String> response = Unirest.get(url)
                .header("User-Agent", "Apifox/1.0.0 (https://www.apifox.cn)")
                .header("Accept", "*/*")
                .header("Host", "www.ibiquge.la")
                .header("Connection", "keep-alive")
                .asString();
//        System.out.println(JSON.toJSONString(response));
        String body = response.getBody();

        return new Html(body, url);
    }


    @Test
    public void test6() throws UnirestException, IOException {
        String str = "他是谁";
        String text = getText(str);
        System.out.println(text);
    }

    private String getText(String str) throws IOException {
        String url = "https://www.youdao.com/w/" + str + "/#keyfrom=dict2.top";
        Document document = Jsoup.connect(url).get();
        if(document.select("a[class=search-js]").size() > 0) {
            return document.select("a[class=search-js]").get(0).text();
        } else {
            return "";
        }
    }

    @Test
    public void test7() throws UnirestException, IOException {
        Map<String,Object> map = new HashMap();
        String content = FileUtils.readFileToString(new File("C:\\Users\\zxk\\Desktop\\temp\\降魔金刚杵.txt"));
        map.put("q",content);
        map.put("from","zh");
        map.put("to","en");
        map.put("appid","20190805000324150");
        map.put("salt","6");
        map.put("sign","93f07dc0eab634ef283d1c922fc92910");
        Unirest.setTimeouts(0, 0);

        HttpResponse<String> response = Unirest.post("https://fanyi-api.baidu.com/api/trans/vip/translate")
                .header("User-Agent", "Apifox/1.0.0 (https://www.apifox.cn)")
                .header("Content-Type", "application/x-www-form-urlencoded")
                .header("Accept", "*/*")
                .header("Host", "fanyi-api.baidu.com")
                .header("Connection", "keep-alive")
                .queryString(map)
                .asString();

        System.out.println(JSON.toJSONString(response));
        FileUtils.writeStringToFile(new File("C:\\Users\\zxk\\Desktop\\temp\\金刚杵.txt"),response.getBody());
    }

}
